package org.ruanjj.longestcommonprefix;

public class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0){
            return "";
        }
        String res = strs[0];

        if(strs.length == 1){
            return res;
        }

        for(int i = 0; i < strs.length; i++){
            int j = Math.min(res.length(),strs[i].length());
            while(!strs[i].substring(0,j).equals(res.substring(0,j)) && j > 0){
                j--;
            }
            if(j > 0){
                res = res.substring(0,j);
            }else{
                res = "";
                break;
            }
        }
        return res;
    }
}
